
import './index.scss'
import { Component, PureComponent, memo, useEffect, useState } from "react";
import { useNavigate } from 'react-router-dom';
import { Button, Form, Input, Space, Table, PageHeader, message } from 'antd';
import { PlusOutlined } from '@ant-design/icons';
import { Switch } from 'antd';
import { getall, del } from '../../../../api/frremember_ssh'
import format from '../../../../utils/formatDay_ssh';
//面包屑
const routes = [
    {
        path: '/',
        breadcrumbName: '用户管理',
    },
    {
        path: '/home/user/free-member',
        breadcrumbName: '免费会员等级管理',
    },

];
//开关
const onChange = (checked) => {
    console.log(`switch to ${checked}`);
};

const FreeMember = () => {
    const [data, setData] = useState()
    const [form] = Form.useForm()
    useEffect(() => {
        getfreemember()
    }, [])
    //初始化
    const getfreemember = () => {
        getall().then((res) => {
            console.log(res);
            let _data = res.data
            _data.forEach(v => {
                v.vipUpdateTime = format(v.vipUpdateTime)
            });
            setData(_data)
        })
    }

    const Navigate = useNavigate()
    //搜索
    const onFinish = (values) => {

        console.log('Success:', values);
        getall(values).then((res) => {
            console.log(res);
            setData(res.data)
        })
    };
    //删除
    const delfreemember = (id) => {
        del(id).then((res) => {
            console.log(res);
            if (res.code == 200) {
                message.success('删除成功');
            } else {
                message.error('删除失败');
            }
            getfreemember()
        })
    }
    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    //编辑
    const mod = () => {
        Navigate('/home/user/Membership_level')
    }
    //新增
    const add = () => {
        Navigate('/home/user/New_members')
    }
    const showTotal = (total) => `数量 ${total} 条`;
    //表格
    const columns = [
        {
            title: '等级',
            dataIndex: 'vipLevel',
            key: 'vipLevel',
            render: (text) => <a>{text}</a>,
        },
        {
            title: '等级名称',
            dataIndex: 'vipName',
            key: 'vipName',
        },
        {
            title: '成长值',
            dataIndex: 'growValue',
            key: 'growValue',
        },
        {
            title: '升级礼包',
            key: 'upgradeGift',
            dataIndex: 'upgradeGift',
        },
        {
            title: '等级状态',
            dataIndex: 'levelState',
            key: 'levelState',
            render: (_, record) => (
                <Space size="middle">
                    < Switch defaultChecked onChange={onChange} checked={record.levelState == '0' ? true : ''} />

                </Space>
            ),
        },
        {
            title: '最后修改时间',
            key: 'vipUpdateTime',
            dataIndex: 'vipUpdateTime',
        },
        {
            title: '操作',
            key: 'action',
            render: (_, record) => (
                <Space size="middle">
                    <a onClick={mod}>编辑</a>
                    <a onClick={() => { delfreemember(record.vipId) }}>删除</a>
                </Space>
            ),
        },
    ];

    return <div className='freemember'>
        {/* 头部 */}
        <div className="header">
            <PageHeader
                className="site-page-header"
                title="免费会员等级管理"
                breadcrumb={{
                    routes,
                }}

            />

            <Form
                form={form}
                layout='inline'
                name="basic"
                labelCol={{
                    span: 8,
                }}
                wrapperCol={{
                    span: 16,
                }}
                initialValues={{
                    remember: true,
                }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="等级名称"
                    name="vipName"

                >
                    <Input placeholder="请输入..." />
                </Form.Item>
                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                    className='btn1'
                >
                    <Button type="primary" htmlType="submit">
                        查询
                    </Button>
                </Form.Item>
                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                    className='btn2'
                >
                    <Button htmlType="submit" onClick={() => {
                        return form.resetFields()
                    }}>
                        重置
                    </Button>
                </Form.Item>
            </Form>
            {/* 以上为头部 */}
        </div>
        {/* 表格 */}
        <div className="content">
            <div className="top">
                <h3>等级列表</h3>
                <Button type="primary" icon={<PlusOutlined />} className='btn3' onClick={add}>新建</Button>
            </div>
            <Table columns={columns} rowKey={(record) => record.vipId} dataSource={data} pagination={{
                total: 10,
                showTotal: showTotal,
                showSizeChanger: true,
                showQuickJumper: true,
            }} />
        </div>
    </div>
}
export default FreeMember;